package com.example.ymm.modules1.service.impl.hl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.yw.YwExcludeFapiaoItem;
import com.example.ymm.modules1.mapper.hl.YwExcludeFapiaoItemMapper;
import com.example.ymm.modules1.query.hl.YwExcludeFapiaoItemQueryCriteria;
import com.example.ymm.modules1.service.dto.heng.BatchExcludeFapiaoDto;
import com.example.ymm.modules1.service.hl.YwExcludeFapiaoItemService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
@DS("slave2")
public class YwExcludeFapiaoItemServiceImpl extends BaseServiceImpl<YwExcludeFapiaoItemMapper, YwExcludeFapiaoItem> implements YwExcludeFapiaoItemService {

    @Override
    public TableList<YwExcludeFapiaoItem> list(YwExcludeFapiaoItemQueryCriteria criteria, YmPageDto pageDto) {
        IPage<YwExcludeFapiaoItem> page = new Page<YwExcludeFapiaoItem>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(YwExcludeFapiaoItem.class, criteria);
//        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<YwExcludeFapiaoItem> tableList=new TableList(page);
        return tableList;
    }

    public List<YwExcludeFapiaoItem> queryAll(YwExcludeFapiaoItemQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(YwExcludeFapiaoItem.class, criteria);
//        predicate.orderByDesc("create_time");
        List<YwExcludeFapiaoItem> result =baseMapper.selectList(predicate);
        return result;
    }


    @Override
    public void batchExcludeFapiaoAdd(BatchExcludeFapiaoDto dto) {
        String userId = getUserId();
        String userName = getUserName();
        List<YwExcludeFapiaoItem> list = dto.getList();
        List<YwExcludeFapiaoItem> addList = new ArrayList<>();
        for (YwExcludeFapiaoItem item : list) {
            YwExcludeFapiaoItem fapiaoItem = getById(item.getOid());
            if(UtilValidate.isEmpty(fapiaoItem)){
//                item.setOrderNo(item.getSkqrNo());
                item.setUserId(userId);
                item.setUserName(userName);
                addList.add(item);
            }
        }
        saveOrUpdateBatch(addList);

    }

    @Override
    public void batchExcludeFapiaoRemove(BatchExcludeFapiaoDto dto) {
        List<YwExcludeFapiaoItem> list = dto.getList();
        for (YwExcludeFapiaoItem item : list) {
            removeById(item.getId());
        }
    }

    @Override
    public List<String> getOrderNo(YwExcludeFapiaoItemQueryCriteria criteria) {
        return baseMapper.getOrderNo(criteria);
    }
}
